<?php
/**
 * Notes:
 * Author: chenjie
 * @return
 * Date: 2021/11/26
 */

namespace App\Http\Controllers\Admin\Erm;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\HelperService;
use Illuminate\Http\Request;

class BlackRemoveController extends BaseController
{

    /**
     * @api {get} /erm/black_record/export 导出移出黑名单日志
     * @apiVersion 0.1.0
     * @apiName 导出移出黑名单日志
     * @apiGroup 黑名单移出管理
     *
     * @apiParam {int} user_code 用户ID，非必传
     * @apiParam {int} nickname 用户昵称，非必传
     * @apiParam {int} user_type 用户类型 1普通用户 2官方管理 3公会会长 4公会厅主 5公会成员，非必传
     * @apiParam {int} admin_id 操作人，非必传
     * @apiParam {int} start_register_time 最大注册时间，非必传
     * @apiParam {int} end_register_time 最小注册时间，非必传
     * @apiParam {int} start_remove_time 移出时间开始，非必传
     * @apiParam {int} end_remove_time 移出时间结束，非必传
     *
     * @apiSuccessExample 成功响应
     *  {
     *   "code": 200,
     *   "data": {
     *       "download_url": "asdasdasd.com"
     *       }
     *  }
     */
    public function export(Request $request)
    {
        $param = $request->only('user_code', 'nickname', 'user_type', 'admin_id', 'start_register_time', 'end_register_time', 'start_remove_time', 'end_remove_time');
        $v = app('validator')->make($param, [
            'user_code' => 'filled|integer',
            'nickname' => 'filled|string|max:64',
            'user_type' => 'filled|integer',
            'admin_id' => 'filled|gt:0',
            'start_register_time' => 'filled|string',
            'end_register_time' => 'filled|string',
            'start_remove_time' => 'filled|string',
            'end_remove_time' => 'filled|string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_erm_black_remove')->getLog($param, 0, 0);

        if ($list) {
            $result = [];
            foreach ($list as $item) {
                $temp = [
                    $item->user_code,
                    "\t" . HelperService::wxNickNameFormat($item->nickname),
                    $item->user_type,
                    $item->register_time,
                    $item->create_time,
                    $item->admin_name,
                ];
                $result[] = $temp;
            }

            $headings = [
                '用户ID',
                '用户昵称',
                '用户类型',
                '注册时间',
                '移出时间',
                '操作人',
            ];

            return app('repo_export')->export(30, $param, $headings, $result);
        }

    }

    /**
     * @api {get} /erm/black_record/list 黑名单移出记录
     * @apiVersion 0.1.0
     * @apiName 黑名单移出记录
     * @apiGroup 黑名单移出管理
     *
     * @apiParam {int} user_code 用户ID，非必传
     * @apiParam {int} nickname 用户昵称，非必传
     * @apiParam {int} user_type 用户类型 1普通用户 2官方管理 3公会会长 4公会厅主 5公会成员，非必传
     * @apiParam {int} admin_id 操作人，非必传
     * @apiParam {int} start_register_time 最大注册时间，非必传
     * @apiParam {int} end_register_time 最小注册时间，非必传
     * @apiParam {int} start_remove_time 移出时间开始，非必传
     * @apiParam {int} end_remove_time 移出时间结束，非必传
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，1-100之间，默认10
     *
     * @apiSuccessExample {json} Response-Example
     *
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "nickname 用户昵称": "今天阳光明媚呀",
    "user_code 用户ID": 343110,
    "user_type 用户类型": "官方管理",
    "register_time 注册时间": "2021-11-17 22:04:11",
    "admin_name": "admin",
    "id": 3,
    "user_id": 643415,
    "recharge_prove 充值证明": [
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png",
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png"
    ],
    "relation_prove 关系证明": [
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png",
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png"
    ],
    "other_prove 其他资料": [],
    "operator_id": 1,
    "join_time": 1637913031,
    "create_time 移出时间": "2021-11-26 17:13:20"
    },
    {
    "nickname": "小气泡2796969",
    "user_code": 2796969,
    "user_type": "普通用户",
    "register_time": "2021-11-17 22:04:11",
    "admin_name": "admin",
    "id": 2,
    "user_id": 1317875,
    "recharge_prove": [],
    "relation_prove": [
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png",
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png"
    ],
    "other_prove": [],
    "operator_id": 1,
    "join_time": 0,
    "create_time": "2021-11-26 15:56:36"
    },
    {
    "nickname": "小气泡6184891",
    "user_code": 6184891,
    "user_type": "普通用户",
    "register_time": "2021-11-17 22:04:11",
    "admin_name": "admin",
    "id": 1,
    "user_id": 1317874,
    "recharge_prove": [
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png",
    "http://sound-river-dev.oss-cn-hangzhou.aliyuncs.com/admin_images/6137126711c0d.png"
    ],
    "relation_prove": [],
    "other_prove": [],
    "operator_id": 1,
    "join_time": 0,
    "create_time": "2021-11-26 15:56:36"
    }
    ],
    "first_page_url": "http://qipao-admin.so/erm/black_record/list?=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://qipao-admin.so/erm/black_record/list?=1",
    "next_page_url": null,
    "path": "http://qipao-admin.so/erm/black_record/list",
    "per_page": 20,
    "prev_page_url": null,
    "to": 3,
    "total": 3
    },
    "message": ""
    }
     *
     */
    public function list(Request $request)
    {
        $param = $request->only('user_code', 'nickname', 'user_type', 'admin_id', 'start_register_time', 'end_register_time', 'start_remove_time', 'end_remove_time', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'user_code' => 'filled|integer',
            'nickname' => 'filled|string|max:64',
            'user_type' => 'filled|integer',
            'admin_id' => 'filled|gt:0',
            'start_register_time' => 'filled|string',
            'end_register_time' => 'filled|string',
            'start_remove_time' => 'filled|string',
            'end_remove_time' => 'filled|string',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 20);

        $list = app('repo_erm_black_remove')->getLog($param, $page, $perPage);
        return Response::sendData($list);
    }

    /**
     * @api {post} /erm/black_record/remove 移出黑名单
     * @apiVersion 0.1.0
     * @apiName 移出黑名单
     * @apiGroup 黑名单移出管理
     *
     * @apiParam {int} user_id 用户ID，必传
     * @apiParam {json} recharge_prove 充值证明图片列表["http:\/\/sound-river-dev.oss-cn-hangzhou.aliyuncs.com\/admin_images\/6137126711c0d.png","http:\/\/sound-river-dev.oss-cn-hangzhou.aliyuncs.com\/admin_images\/6137126711c0d.png"]，必传
     * @apiParam {json} relation_prove 关系证明图片列表，必传
     * @apiParam {json} other_prove 其他证明，非必传
     *
     * @apiSuccessExample {json} Response-Example
     *
     * {
    "code": 200,
    "data": [],
    "message": ""
    }
     */
    public function remove(Request $request)
    {
        $param = $request->only('user_id', 'recharge_prove', 'relation_prove', 'other_prove');
        $v = app('validator')->make($param, [
            'user_id' => 'required|integer|gt:0',
            'recharge_prove' => 'required|string',
            'relation_prove' => 'required|string',
            'other_prove' => 'string'
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['operator_id'] = $request->admin_id ?? '';

        app('repo_erm_black_remove')->remove($param);
        return Response::sendData([]);
    }

}